package com.chx.service.Impl;

import com.chx.constant.MessageConstant;
import com.chx.dto.ProductDto;
import com.chx.dto.ProductPageQueryDto;
import com.chx.entity.Products;
import com.chx.exception.DeletionNotAllowedException;
import com.chx.mapper.*;
import com.chx.result.PageResult;
import com.chx.service.ProductService;
import com.chx.wrapper.ProductWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author cp-月入过万
 * @Date 2025/5/30
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private PurchaseOrderDetailMapper  purchaseOrderDetailMapper;
    @Autowired
    private SaleOrderDetailMapper saleOrderDetailMapper;
    @Autowired
    private StockInOrderDetailMapper stockInOrderDetailMapper;
    @Autowired
    private StockOutOrderDetailMapper stockOutOrderDetailMapper;
    @Override
    public List<Products> getAllProducts() {
        return productMapper.getAllProducts();
    }

    @Override
    public void save(ProductDto productDto) {
        Products products = new Products();
        BeanUtils.copyProperties(productDto, products);
        productMapper.insert(products);
    }

    @Override
    public void update(ProductDto productDto) {
        Products products = new Products();
        BeanUtils.copyProperties(productDto, products);
        productMapper.update(products);
    }

    @Override
    public void delete(Long id) {
        boolean  flag = purchaseOrderDetailMapper.getByProductId(id)>0 || saleOrderDetailMapper.getByProductId(id)>0 || stockInOrderDetailMapper.getByProductId(id)>0 || stockOutOrderDetailMapper.getByProductId(id)>0;
        if(flag)
            throw new DeletionNotAllowedException(MessageConstant.DELETE_HA_ORDER);
        productMapper.delete(id);
    }

    @Override
    public Products getById(Long id) {
        return productMapper.getById(id);
    }

    @Override
    public PageResult page(ProductPageQueryDto productPageQueryDto) {
        PageHelper.startPage(productPageQueryDto.getPage(), productPageQueryDto.getPageSize());
        Page<Products> page = productMapper.page(productPageQueryDto);
        long  total = page.getTotal();
        List<Products> records = page.getResult();
        List<ProductWrapper> productWrappers = records.stream().map(record -> {
            String categoryName = categoryMapper.getById(record.getCategoryId()).getCategoryName();
            String supplierName = supplierMapper.getById(record.getSupplierId()).getSupplierName();
            return new ProductWrapper(record, categoryName, supplierName);
        }).collect(Collectors.toList());
        return new PageResult(total, productWrappers);
    }
}
